package com.example.my_flea_street.controller;

import com.example.my_flea_street.bean.Info;
import com.example.my_flea_street.bean.InfoPage;
import com.example.my_flea_street.bean.ResponseModel;
import com.example.my_flea_street.bean.User;
import com.example.my_flea_street.service.EditService;
import com.example.my_flea_street.service.impl.EditServiceImpl;
import com.example.my_flea_street.service.impl.UserViewServiceImpl;
import com.example.my_flea_street.util.ImageUtil;
import com.example.my_flea_street.util.ResponseHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    private final UserViewServiceImpl userService;
    private final EditServiceImpl editService;

    @Autowired
    public UserController(UserViewServiceImpl userService, EditServiceImpl editService) {
        this.userService = userService;
        this.editService = editService;
    }

    @RequestMapping(value = "/getUserInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> getInfo(@RequestBody Map<String, String> json) {
        int pageNum = Integer.parseInt(json.get("page_no"));
        int pageSize = Integer.parseInt(json.get("page_size"));
        int user_id = Integer.parseInt(json.get("user_id"));
        int type = Integer.parseInt(json.get("type"));
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Info> pageInfo = new PageInfo<>(userService.getInfo(user_id, type));
        InfoPage page = new InfoPage();
        page.setTotal((int) pageInfo.getTotal());
        page.setPages(pageInfo.getPages());
        page.setPageNum(pageInfo.getPageNum());
        page.setData(pageInfo.getList());
        page.setSettledCount(userService.getSettledCount(user_id, 1));
        return ResponseHelper.createResponse(0, "成功", page);
    }

    @RequestMapping(value = "/getUserById", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> getUserById(@RequestBody Map<String, String> json) {
        int userId = Integer.parseInt(json.get("user_id"));
        User user = userService.getUserById(userId);
        if (user == null) {
            return ResponseHelper.createResponse(-1,"用户查询失败！");
        }
        return ResponseHelper.createResponse(0,"成功",user);
    }
    @RequestMapping(value = "/deleteInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> deleteInfo(@RequestBody Map<String, String> json) {
        int id = Integer.parseInt(json.get("id"));
        String picUrl = editService.getPic(id);
        if (picUrl != null && !picUrl.equals("") && !"null".equals(picUrl))
            ImageUtil.deletePic(picUrl,"info");
        userService.deleteInfo(id);
        return ResponseHelper.createResponse(0,"成功");
    }
    @RequestMapping(value = "/settledInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> settledInfo(@RequestBody Map<String, String> json) {
        int id = Integer.parseInt(json.get("id"));
        userService.settledInfo(id);
        return ResponseHelper.createResponse(0,"成功");
    }

    @RequestMapping(value = "/jubaoUser",method = {RequestMethod.POST,RequestMethod.GET})
    public ResponseEntity<ResponseModel> jubaoUser(@RequestBody Map<String, String> json) {
        String release_time = json.get("release_time");
        String text = json.get("text");
        int userId = Integer.parseInt(json.get("user_id"));
        String picBase64 = json.get("pic");
        String newPic = "";
        if (picBase64!=null && !picBase64.equals(""))
            newPic = ImageUtil.GenerateImage(picBase64,release_time,"report");
        userService.report(userId,text,release_time,newPic);
        return ResponseHelper.createResponse(0, "成功");
    }
}
